package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.CountryInfo;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.MchInfoVo;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.listcountryBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.createmchCall;
import com.ymatou.iapi.godmch.service.listcountryCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.country_info;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
/**
*获取国家列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_listcountry{
	private static listcountryBean bean;
	private static listcountryCall call;
	DefGodMchWapper DefGodMchWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取国家列表");
	}

	@Before
	public void caseUp() {
		bean=new listcountryBean();
		call=new listcountryCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public static boolean isHave(String[] strs, String s) {
		/*
		 * 此方法有两个参数，第一个是要查找的字符串数组，第二个是要查找的字符或字符串
		 */
		for (int i = 0; i < strs.length; i++) {
			if (strs[i].indexOf(s) != -1) {// 循环查找字符串数组中的每个字符串中是否包含所有查找的内容
				return true;// 查找到了就返回真，不在继续查询
			}
		}
		return false;// 没找到返回false
	}
//	/**
//	 * 过滤不存在的国家
//	 * @throws InterruptedException 
//	 */
//	private String[] filterCountry( String[] dbCountryList) throws InterruptedException
//	{
//		List<country_info> dbCountryInfos = DefGodMchWapper.selectCountryInfo(null,null);
//		
//        if(dbCountryList.length>dbCountryInfos.size())
//        {
//        
//        }
//        else {
//			
//		}
//		
//    	for(int i=0;i<dbCountryInfos.size();i++)
//		{
//			if(!dbCountryList[i].equals(dbCountryInfos.get(i)))
//			{
//				StringUtils.strip(dbCountryList.toString(),"[]");
//			}
//		}
//	}
	private void checkVerify(String currency) throws InterruptedException, JSONException, InterruptedException 
	{
			List<country_info> dbCountryInfos = DefGodMchWapper.selectCountryInfo(null,currency);
			
			ArrayList<CountryInfo> rcinfos = call.getCountryInfos();
			Logger.debug("dbCountryInfos:"+FormatUtil.beanToGSONString(dbCountryInfos));
			Logger.debug("rcinfos:"+FormatUtil.beanToGSONString(rcinfos));
			
            Logger.comment("长度："+rcinfos.size());
            String[] dbCountryList=dbCountryInfos.get(0).getSupportAgentCountry().replace("SGP,", "").split(",");
           if(dbCountryList.length==1)
           {
        	   List<country_info> countryAll = DefGodMchWapper.selectCountryInfo(null,null);
        	   Logger.verifyEquals(countryAll.size(), rcinfos.size(), "验证支持币种为空，显示所有的国家");
           }
           else 
               Logger.verifyEquals(dbCountryList.length, rcinfos.size(), "验证返回的国家数与数据库查询的一致");
			
          //国家信息
            rcinfos.forEach(rcountryInfo->{
            	country_info dbCountryInfos2=new country_info();
				try {
					dbCountryInfos2 = DefGodMchWapper.selectCountryInfo(rcountryInfo.getCountry(),null).get(0);
				} catch (Exception e) {
					e.printStackTrace();
				}
				Logger.verifyEquals(dbCountryInfos2.getCnTitle(), rcountryInfo.getCnTitle(), "验证getCnTitle");
				Logger.verifyEquals(dbCountryInfos2.getContinent(), rcountryInfo.getContinent(), "验证getContinent");
				Logger.verifyEquals(dbCountryInfos2.getCountry(), rcountryInfo.getCountry(), "验证Country");
				Logger.verifyEquals(dbCountryInfos2.getCurrency(), rcountryInfo.getCurrency(), "验证Currency");
				Logger.verifyEquals(dbCountryInfos2.getAreaCode(), rcountryInfo.getAreaCode(), "验证AreaCode");
				Logger.verifyEquals(dbCountryInfos2.getEnTitle(), rcountryInfo.getEnTitle(), "验证EnTitle");
				
				if(dbCountryList.length>1)
				  Logger.verifyEquals(true,isHave(dbCountryList,rcountryInfo.getCountry()), "验证数据库支持的代理国家 与 返回的代理国家一致");
				
			});
			
//			//国家信息
//			dbCountryInfos.forEach(dbCountryInfo->{
//				CountryInfo rcountryInfo = rcinfos.stream().filter(ci->ci.getCountryCode().equals(dbCountryInfo.getCountryCode())).findFirst().get();
//				Logger.verifyEquals(dbCountryInfo.getCnTitle(), rcountryInfo.getCnTitle(), "验证getCnTitle");
//				Logger.verifyEquals(dbCountryInfo.getContinent(), rcountryInfo.getContinent(), "验证getContinent");
//				Logger.verifyEquals(dbCountryInfo.getCountry(), rcountryInfo.getCountry(), "验证Country");
//				Logger.verifyEquals(dbCountryInfo.getCurrency(), rcountryInfo.getCurrency(), "验证Currency");
//				Logger.verifyEquals(dbCountryInfo.getAreaCode(), rcountryInfo.getAreaCode(), "验证AreaCode");
//				Logger.verifyEquals(dbCountryInfo.getEnTitle(), rcountryInfo.getEnTitle(), "验证EnTitle");
//				
//				Logger.verifyEquals(true,isHave(dbCountryList,rcountryInfo.getCountry()), "验证数据库支持的代理国家 与 返回的代理国家一致");
//				
//			});
			
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_001() {
		Logger.start(true,"获取国家列表,验证结算币种为空,返回所有国家");
		try {
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			checkVerify(bean.getCurrency());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_002() {
		Logger.start(true,"获取国家列表,验证结算币种非空,返回所属结算币种国家列表");
		try {
			bean.setCurrency("CNY");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			checkVerify(bean.getCurrency());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_003() {
		Logger.start(true,"获取国家列表,验证结算币种不存在,返回错误,提示结算币种不存在");
		try {
			bean.setCurrency("ATS");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyEquals("{}",new org.json.JSONObject(call.getReturnData().toString()).get("result").toString(), "验证结算币种不存在，返回空");
			//checkVerify(bean.getCurrency());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_004() {
		Logger.start(true,"获取国家列表,Currency欧元多个国家使用");
		try {
			bean.setCurrency("EUR");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			checkVerify(bean.getCurrency());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_005() {
		Logger.start(true,"获取国家列表,币种与邀请码 不一致");
		try {
			createmchBean cmBean=new createmchBean();
			createmchCall cmcall=new createmchCall();
			int userId1 = GodmchManageCallService.UserCreateAgent();
			// 创建渠道
			cmBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId1);
			cmBean.setCountry("HKG");
			GodmchManageCallService.createmchCallForChannel(cmBean);
			// 渠道商邀请码
			CreateCodeBean createCodeBean = GodmchManageCallService.InitcreateCodeBeanForAgent(userId1);
			CreateCodeCall createcall = GodmchManageCallService.createCodeCall(createCodeBean);
			
			bean.setCurrency("HKD");
			bean.setInviteCode(createcall.getCode());
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals("200", call.getString("status"), "验证status");
			checkVerify(bean.getCurrency());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_006() {
		Logger.start(true,"获取国家列表,特殊渠道是香港，代理日本-返回当前币种国家");
		try {
			createmchBean cmBean=new createmchBean();
			createmchCall cmcall=new createmchCall();
			int mchId=500182875;//这个用户是香港
//			int userId1 = GodmchManageCallService.UserCreateAgent();
//			// 创建渠道
//			cmBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId1);
//			cmBean.setCountry("HKG");
//			GodmchManageCallService.createmchCallForChannel(cmBean);
			// 渠道商邀请码
			CreateCodeBean createCodeBean = GodmchManageCallService.InitcreateCodeBeanForAgent(mchId);
			createCodeBean.setIsPermanently(false);
			CreateCodeCall createcall = GodmchManageCallService.createCodeCall(createCodeBean);
			//如果Currency和InviteCode同时存在，以InviteCode为主
			bean.setCurrency("JPY");//日本 JPY  中国香港 HKD
			bean.setInviteCode(createcall.getCode());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			//
			mch_info mchInfo=DefGodMchWapper.selectMchInfo(mchId).get(0);
			checkVerify(mchInfo.getCurrency());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_007() {
		Logger.start(true,"获取国家列表,特殊渠道是香港，代理日本-币种为空,返回所有国家(开发邀请码业务注释掉，)");
		try {
			createmchBean cmBean=new createmchBean();
			createmchCall cmcall=new createmchCall();
			int mchId=500182875;//这个用户是香港
//			int userId1 = GodmchManageCallService.UserCreateAgent();
//			// 创建渠道
//			cmBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId1);
//			cmBean.setCountry("HKG");
//			GodmchManageCallService.createmchCallForChannel(cmBean);
			// 渠道商邀请码
			CreateCodeBean createCodeBean = GodmchManageCallService.InitcreateCodeBeanForAgent(mchId);
			createCodeBean.setIsPermanently(false);
			CreateCodeCall createcall = GodmchManageCallService.createCodeCall(createCodeBean);
			
			//bean.setCurrency("JPY");//日本 JPY  中国香港 HKD
			bean.setInviteCode(createcall.getCode());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			checkVerify(null);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_listcountry_008() {
		Logger.start(true,"获取国家列表,USA,支持代理所有国家");
		try {
			bean.setCurrency("USD");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			checkVerify(bean.getCurrency());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}